package com.kaiwukj.android.ufamily.mvp.ui.page.service.repairs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.mvp.ui.adapter.HomeViewPagerAdapter;
import com.kaiwukj.android.ufamily.mvp.ui.widget.home.ScaleTransitionPagerTitleView;
import com.qmuiteam.qmui.widget.QMUITopBar;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt.RepairsRouterUrl;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 * 报修
 */
@Route(path = RepairsRouterUrl)
public class RepairsActivity extends BaseActivity {

    @BindView(R.id.qtb_complain)
    QMUITopBar qtbTitle;
    @BindView(R.id.magic_indicator_complain)
    MagicIndicator miIndicator;
    @BindView(R.id.view_pager_complain_container)
    ViewPager vpContainer;

    private List<Fragment> mRepairsFragmentList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_complain;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        qtbTitle.setTitle("物业报修");
        qtbTitle.addLeftBackImageButton().setOnClickListener(view -> finish());

        List<String> magicIndicatorContentList = new ArrayList<>();
        magicIndicatorContentList.add("我要报修");
        magicIndicatorContentList.add("报修记录");
        initMagicIndicatorView(magicIndicatorContentList);
    }

    public void initMagicIndicatorView(List<String> magicIndicatorContentList) {
        CommonNavigator mMIndicatorNavigator = new CommonNavigator(this);
        mMIndicatorNavigator.setLeftPadding(10);
        mMIndicatorNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return magicIndicatorContentList == null ? 0 : magicIndicatorContentList.size();
            }

            @Override
            public IPagerTitleView getTitleView(final Context context, int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                simplePagerTitleView.setText(magicIndicatorContentList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.home_color_hot_service_text));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.common_text_dark_color));
                simplePagerTitleView.setOnClickListener(v -> vpContainer.setCurrentItem(index));
                return simplePagerTitleView;

            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setRoundRadius(20);
                indicator.setLineHeight(10f);
                indicator.setLineWidth(80f);
                indicator.setYOffset(60f);
                indicator.setColors(ContextCompat.getColor(context, R.color.common_text_dark_color));
                return indicator;
            }
        });

        mRepairsFragmentList.add(RepairsFragment.newInstance());
        mRepairsFragmentList.add(RepairsListFragment.newInstance());
        miIndicator.setNavigator(mMIndicatorNavigator);
        ViewPagerHelper.bind(miIndicator, vpContainer);
        HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getSupportFragmentManager(), mRepairsFragmentList);
        vpContainer.setAdapter(homeViewPagerAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mRepairsFragmentList.get(vpContainer.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void post(Runnable runnable) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

}
